package cn.bandeqing.frame.mybatis.config;


import cn.bandeqing.base.domain.CurrentAdminUser;
import cn.bandeqing.base.domain.CurrentWechatMiniAppUser;
import cn.bandeqing.base.domain.CurrentWorkerUser;
import cn.bandeqing.base.util.Constants;
import cn.bandeqing.frame.dubbo.DubboProviderFilter;
import cn.bandeqing.frame.mybatis.entity.BaseEntity;
import cn.bandeqing.stp.StpAdminUtil;
import cn.bandeqing.stp.StpWechatMiniAppUtil;
import cn.bandeqing.stp.StpWorkerUtil;
import com.mybatisflex.annotation.InsertListener;
import com.mybatisflex.annotation.UpdateListener;
import org.apache.dubbo.metrics.observation.DubboServerContext;
import org.apache.dubbo.rpc.RpcContext;
import org.dromara.hutool.core.util.ObjUtil;

public class InsertOrUpdateListener implements UpdateListener, InsertListener {

    @Override
    public void onInsert(Object entity) {
        if (entity instanceof BaseEntity be) {
            if (ObjUtil.isNull(be.getCreateUserId())) {
                String appName = DubboProviderFilter.APP_NAME.get();
//                String appName = RpcContext.getServerAttachment().getAttachment(Constants.appName);
                if ("bdq-wechat-miniapp-api".equals(appName)) {
                    CurrentWechatMiniAppUser current = StpWechatMiniAppUtil.getCurrent();
                    be.setCreateUserId(current.getCustomerId());
                    be.setCreateUserName(current.getCustomerNickName());
                } else if ("bdq-admin-api".equals(appName)) {
                    CurrentAdminUser current = StpAdminUtil.getCurrent();
                    be.setCreateUserId(current.getUsId());
                    be.setCreateUserName(current.getUsNickName());
                } else if ("bdq-worker-app-api".equals(appName)) {
                    CurrentWorkerUser current = StpWorkerUtil.getCurrent();
                    be.setCreateUserId(current.getWorkerId());
                    be.setCreateUserName(current.getWorkerNickName());
                }
            }
        }
    }

    @Override
    public void onUpdate(Object entity) {
        if (entity instanceof BaseEntity be) {
            if (ObjUtil.isNull(be.getUpdateUserId())) {
                String appName = DubboProviderFilter.APP_NAME.get();
                if ("bdq-wechat-miniapp-api".equals(appName)) {
                    CurrentWechatMiniAppUser current = StpWechatMiniAppUtil.getCurrent();
                    be.setUpdateUserId(current.getCustomerId());
                    be.setUpdateUserName(current.getCustomerNickName());
                } else if ("bdq-admin-api".equals(appName)) {
                    CurrentAdminUser current = StpAdminUtil.getCurrent();
                    be.setUpdateUserId(current.getUsId());
                    be.setUpdateUserName(current.getUsNickName());
                } else if ("bdq-worker-app-api".equals(appName)) {
                    CurrentWorkerUser current = StpWorkerUtil.getCurrent();
                    be.setUpdateUserId(current.getWorkerId());
                    be.setUpdateUserName(current.getWorkerNickName());
                }
            }
        }
    }

}
